package com.tjhzzx.ibmsSupervisor.servlet;

import com.tridium.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class JSONResult {
    //0：成功；1：失败
    private int state;
    private String message;
    private Map<String, Object> data = new HashMap<>();

    public static JSONResult success() {
        JSONResult resultMsg = new JSONResult();
        resultMsg.setState(0);
        resultMsg.setMessage("处理成功");
        return resultMsg;
    }

//    public boolean isSuccess() {
//        return state == 0;
//    }
//
//    public boolean isError() {
//        return state == 1;
//    }

    public static JSONResult success(String msg) {
        JSONResult resultMsg = new JSONResult();
        resultMsg.setState(0);
        resultMsg.setMessage(msg);
        return resultMsg;
    }

    public static JSONResult error() {
        JSONResult resultMsg = new JSONResult();
        resultMsg.setState(1);
        resultMsg.setMessage("处理失败");
        return resultMsg;
    }

    public static JSONResult error(String msg) {
        JSONResult resultMsg = new JSONResult();
        resultMsg.setState(1);
        resultMsg.setMessage(msg);
        return resultMsg;
    }

    public JSONResult add(String key, Object value) {
        data.put(key, value);
        return this;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Map<String, Object> getData() {
        return data;
    }

//    public JSONResult putInData(Object value) {
//        data.put("data", value);
//        return this;
//    }

    public String toJSONString() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("state", state);
        jsonObject.put("message", message);
        jsonObject.put("data", data);
//        for(String key : data.keySet()) {
//            jsonObject.put(key, data.get(key));
//        }
        return jsonObject.toString();
    }

}
